# encoding:utf-8
from elasticsearch import Elasticsearch

es = Elasticsearch(hosts='http://127.0.0.1:9200')


def restart():
    es.indices.delete(index='bid')
    res = es.indices.create(index='bid',
                            settings={
                                "number_of_shards": 5,  # 分片数
                                "number_of_replicas": 1,  # 备份，根据集群
                                "analysis": {  # 指定默认分词器
                                    "analyzer": {
                                        "ik": {
                                            "tokenizer": "ik_smart"
                                        }
                                    },
                                    "search_analyzer": {  # 指定默认搜索分词器
                                        "ik": {
                                            "tokenizer": "ik_smart"
                                        }
                                    }
                                }
                            },
                            mappings={
                                "properties": {
                                    "title": {
                                        "type": "text",
                                        "analyzer": "ik_smart",
                                        "search_analyzer": "ik_smart",
                                        "boost": 5,
                                    },
                                    "content": {
                                        "type": "text",
                                        "analyzer": "ik_smart",
                                        "search_analyzer": "ik_smart",
                                    },
                                    "link": {
                                        "type": "text",
                                        "analyzer": "ik_smart",
                                        "search_analyzer": "ik_smart",
                                    },
                                    "classification": {
                                        "type": "text",
                                        "analyzer": "ik_smart",
                                        "search_analyzer": "ik_smart",
                                    },
                                    "typ": {
                                        "type": "keyword",
                                    },
                                    "time": {
                                        "type": "date",
                                        "format": "yyyy-MM-dd"  # 格式化时间统一格式
                                    }
                                }
                            }
                            )
    print(res)


restart()
